﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Comp476Project
{
    public class Camera
    {
        public Matrix view;
        public float distance, sideAngle, verticalAngle, moveSpeed, tiltCap, zoomSpeed, maxZoomOut, maxZoomIn, slowZoom;
        public Vector3 pos, focus, up;
        public KeyboardState kb;
        public MouseState ms, prevMs;

        public Camera()
        {
            this.distance = 0.2f;
            this.slowZoom = 7f;
            this.pos = new Vector3(0, 0, distance);
            pos = Vector3.Transform(new Vector3(0, 0, distance), Matrix.CreateRotationX(verticalAngle));
            this.focus = new Vector3(0, 0, 0);
            this.up = new Vector3(0, 1, 0);
            this.view = Matrix.CreateLookAt(pos, focus, up);
            this.sideAngle = MathHelper.ToRadians(0f);
            this.verticalAngle = MathHelper.ToRadians(-30.0f);
            this.moveSpeed = 2.0f;
            this.zoomSpeed = 0.5f;
            this.tiltCap = 85f;
            this.kb = Keyboard.GetState();
            this.ms = Mouse.GetState();
            this.prevMs = ms;
            this.maxZoomOut = 30f;
            this.maxZoomIn = 0.1f;
            pos = Vector3.Transform(new Vector3(0, 0, distance), Matrix.CreateRotationX(verticalAngle));
        }

        public void Update(Vector3 focus)
        {
            kb = Keyboard.GetState();
            prevMs = ms;
            ms = Mouse.GetState();
            // Move Camera Right-Left
            //if (kb.IsKeyDown(Keys.Right))
            //    sideAngle += MathHelper.ToRadians(moveSpeed);
            //if (kb.IsKeyDown(Keys.Left))
            //    sideAngle += MathHelper.ToRadians(-moveSpeed);
            // Move Camera Up-Down
            //if (kb.IsKeyDown(Keys.Up))
            //    verticleAngle += MathHelper.ToRadians(-moveSpeed);
            //if (kb.IsKeyDown(Keys.Down))
            //    verticleAngle += MathHelper.ToRadians(moveSpeed);
            // Move With Mouse
            if (ms.LeftButton == ButtonState.Pressed || ms.RightButton == ButtonState.Pressed)
            {
                if (ms.RightButton != ButtonState.Pressed)
                    sideAngle -= MathHelper.ToRadians(moveSpeed / 5 * (ms.X - prevMs.X));
                verticalAngle -= MathHelper.ToRadians(moveSpeed / 5 * (ms.Y - prevMs.Y));
                //if (verticleAngle > MathHelper.ToRadians(-2.5f))
                //    focus.Y -= 0.01f * (ms.Y - prevMs.Y);
                //Mouse.SetPosition(ms.X, ms.Y);
            }
            //else if (focus.Y > 0)
            //{
            //    focus.Y -= 0.01f;
            //}
            // Control Angles
            sideAngle = sideAngle % MathHelper.ToRadians(360f);
            if (verticalAngle > MathHelper.ToRadians(-2.5f))
                verticalAngle = MathHelper.ToRadians(-2.5f);
            if (verticalAngle < MathHelper.ToRadians(-tiltCap))
                verticalAngle = MathHelper.ToRadians(-tiltCap);
            // Zoom In-Out
            if (ms.ScrollWheelValue > prevMs.ScrollWheelValue)
            {
                float zSpeed = (distance / slowZoom) * zoomSpeed;
                distance -= zSpeed;
            }
            if (ms.ScrollWheelValue < prevMs.ScrollWheelValue)
            {
                float zSpeed = (distance / slowZoom) * zoomSpeed;
                distance += zSpeed;
            }
            // Control distance
            if (distance > maxZoomOut)
                distance = maxZoomOut;
            if (distance < maxZoomIn)
                distance = maxZoomIn;
            // Focus
            this.focus = focus;
            pos = Vector3.Transform(new Vector3(0, 0, distance), Matrix.CreateRotationX(verticalAngle) * Matrix.CreateRotationY(sideAngle) * Matrix.CreateTranslation(focus));
            view = Matrix.CreateLookAt(pos, focus, up);
        }
    }
}
